<template>
  <div class="PermissionList">
    <a-row :gutter="12" class="aRow-wrapper">
      <a-col :span="24" class="listContent-wrapper">
        <div class="l-w_content">
          <top-title :content="content" :updateTime="true"></top-title>
          <div class="l-w_c_content">
            <div class="pageInfo">
              <!-- 操作按钮区域 -->
              <div class="table-operator">
                <a-button class="BtnStyle" icon="plus" type="primary" @click="handleAdd">新增</a-button>
                <a-button
                  v-if="selectedRowKeys.length > 0"
                  class="BtnStyle"
                  icon="delete"
                  type="primary"
                  @click="batchDel"
                >
                  批量删除
                </a-button>
              </div>
              <!-- table区域-begin -->
              <div>
                <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
                  <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{
                    selectedRowKeys.length
                  }}</a>
                  项&nbsp;&nbsp;
                  <a style="margin-left: 24px" @click="onClearSelected">清空</a>
                </div>

                <a-table
                  :columns="columns"
                  :dataSource="dataSource"
                  :expandedRowKeys="expandedRowKeys"
                  :loading="loading"
                  :pagination="false"
                  :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
                  :scroll="{ x: 1500 }"
                  class="tableList"
                  size="middle"
                  @expandedRowsChange="handleExpandedRowsChange"
                >
                  <span slot="action" slot-scope="text, record">
                    <a class="operation" @click="handleEdit(record)">编辑</a>
                    <a-divider type="vertical" />
                    <a-dropdown>
                      <a class="ant-dropdown-link operation"> 更多 <a-icon type="down" /> </a>
                      <a-menu slot="overlay">
                        <a-menu-item>
                          <a href="javascript:;" @click="handleDetail(record)">详情</a>
                        </a-menu-item>
                        <a-menu-item>
                          <a href="javascript:;" @click="handleAddSub(record)">添加下级</a>
                        </a-menu-item>
                        <a-menu-item>
                          <a href="javascript:;" @click="handleDataRule(record)">数据规则</a>
                        </a-menu-item>

                        <a-menu-item>
                          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                            <a>删除</a>
                          </a-popconfirm>
                        </a-menu-item>
                      </a-menu>
                    </a-dropdown>
                  </span>
                  <!-- 字符串超长截取省略号显示 -->
                  <span slot="url" slot-scope="text">
                    <j-ellipsis :length="25" :value="text" />
                  </span>
                  <!-- 字符串超长截取省略号显示-->
                  <span slot="component" slot-scope="text">
                    <j-ellipsis :value="text" />
                  </span>
                </a-table>
              </div>
              <!-- table区域-end -->
            </div>
          </div>
        </div>
      </a-col>
    </a-row>

    <permission-modal ref="modalForm" @refresh="modalFormOk"></permission-modal>
    <permission-data-rule-list ref="PermissionDataRuleList" @refresh="modalFormOk"></permission-data-rule-list>
  </div>
</template>

<script>
import PermissionModal from './modules/PermissionModal'
import { getPermissionList } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import PermissionDataRuleList from './PermissionDataRuleList'
import JEllipsis from '@/components/jeecg/JEllipsis'
import topTitle from '@/components/topTitle'

const columns = [
  {
    title: '菜单名称',
    dataIndex: 'name',
    key: 'name',
    align: 'center',
  },
  {
    title: '菜单类型',
    dataIndex: 'menuType',
    align: 'center',
    key: 'menuType',
    customRender: function (text) {
      if (text == 0) {
        return '菜单'
      } else if (text == 1) {
        return '菜单'
      } else if (text == 2) {
        return '按钮/权限'
      } else {
        return text
      }
    },
  },
  /*{
      title: '权限编码',
      dataIndex: 'perms',
      key: 'permissionCode',
    },*/ {
    title: 'ICON',
    dataIndex: 'icon',
    align: 'center',
    key: 'icon',
  },
  {
    title: '组件',
    dataIndex: 'component',
    align: 'center',
    key: 'component',
    scopedSlots: { customRender: 'component' },
  },
  {
    title: '路径',
    dataIndex: 'url',
    align: 'center',
    key: 'url',
    scopedSlots: { customRender: 'url' },
  },
  {
    title: '排序',
    dataIndex: 'sortNo',
    align: 'center',
    key: 'sortNo',
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    fixed: 'right',
    scopedSlots: { customRender: 'action' },
    width: 150,
  },
]

export default {
  name: 'PermissionList',
  mixins: [JeecgListMixin],
  components: {
    PermissionDataRuleList,
    PermissionModal,
    JEllipsis,
    topTitle,
  },
  data() {
    return {
      content: [['菜单管理']],
      description: '这是菜单管理页面',
      // 表头
      columns: columns,
      loading: false,
      // 展开的行，受控属性
      expandedRowKeys: [],
      url: {
        list: '/sys/permission/list',
        delete: '/sys/permission/delete',
        deleteBatch: '/sys/permission/deleteBatch',
      },
    }
  },
  methods: {
    loadData() {
      this.dataSource = []
      getPermissionList().then((res) => {
        if (res.code == 200) {
          console.log(res.result)
          this.dataSource = res.result
        }
      })
    },
    // 打开数据规则编辑
    handleDataRule(record) {
      this.$refs.PermissionDataRuleList.edit(record)
    },
    handleAddSub(record) {
      this.$refs.modalForm.title = '添加子菜单'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.edit({ status: '1', permsType: '1', route: true, parentId: record.id, menuType: 1 })
    },
    handleExpandedRowsChange(expandedRows) {
      this.expandedRowKeys = expandedRows
      console.log(expandedRows, 'expandedRows')
    },
  },
}
</script>
<style lang="less">
@import '~@/assets/less/common.less';
.PermissionList {
  width: 100%;
  height: 100%;
  background: var(--theme-content-bg);
  .aRow-wrapper {
    height: auto;
  }
  .pageInfo {
    width: 100%;
    height: 100%;
  }
  .listContent-wrapper .l-w_content {
    margin-left: 0;
  }
}
</style>
